﻿using System;
using StockModel;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicators.DateBased
{
	public class BarGenerator : GeneratorBase<DatedSeriePoint, Bar>
	{
		#region Attributes

		private TimeSpan _barDuration;
		private Bar _currentBar;

		#endregion

		#region Public Methods

		public BarGenerator()
		{
		}

		public BarGenerator(ObservableCollection2<Bar> destination) : base(destination)
		{
		}

		public void Configure(TimeSpan barDuration)
		{
			_barDuration = barDuration;
		}

		public override void Clear()
		{
			_currentBar = null;

			base.Clear();
		}

		protected override Bar CreatePoint(DatedSeriePoint point)
		{
			if (_currentBar == null || point.X - _currentBar.Start >= _barDuration)
			{
				_currentBar = new Bar(point.X, point.Y);
				return _currentBar;
			}
			else
			{
				_currentBar.AddValue(point.X, point.Y);
				return null;
			}
		}

		#endregion
	}
}
